import { forEach } from '../utils'
interface handlers<V> {
    fulfilled: (value: V) => V | Promise<V>
    rejected: (error: any) => any
}
export default class InterceptorManager<V> {
    
    handlers: handlers<V>[]
    constructor() {
        this.handlers = []
    }

    use(fulfilled: (value: V) => V | Promise<V>, rejected?: (error: any) => any) {
        this.handlers.push({
            fulfilled: fulfilled,
            rejected: rejected
        });
        return this.handlers.length - 1;
    }

    eject(index: number) {
        if (this.handlers[index]) {
            this.handlers[index] = null;
        }
    }
    forEach(fn: Function) {
        forEach(this.handlers, function forEachHandler(h) {
            if (h !== null) {
                fn(h);
            }
        });
    }
}